package com.studentstreet;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.studentstreet.adapter.FavoriteAdapter;
import com.studentstreet.entities.GoodsEntity;
import com.studentstreet.tools.PreferenceTools;
import com.studentstreet.tools.dbhelper.GoodsDBHelper;

/**
 * @author likebamboo
 * 
 */
public class FavoritesActivity extends ListActivity {

	private ListView favoriteListView = null;
	private List<GoodsEntity> favoriteList = null;
	private FavoriteAdapter adapter = null;

	private String userName = "";
	private PreferenceTools pref = null;
	private GoodsDBHelper dbHelper = null;
	private LinearLayout goCartLayout = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 设置当前Activity无标题
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		// 设置布局
		setContentView(R.layout.favorites);
		// 将当前Activity压入堆栈中
		MyApp.getInstance().addActivity(this);

		pref = new PreferenceTools(this);
		dbHelper = new GoodsDBHelper(this);

		favoriteList = new ArrayList<GoodsEntity>();
		favoriteListView = getListView();
		adapter = new FavoriteAdapter(this, favoriteListView);

		favoriteListView.setAdapter(adapter);
		// 设置ItemOnClick监听器
		favoriteListView
				.setOnItemClickListener(new AdapterView.OnItemClickListener() {
					@Override
					public void onItemClick(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
						Intent intent = new Intent(FavoritesActivity.this,
								GoodsDetailActivity.class);
						GoodsEntity goods = favoriteList.get(arg2);
						Bundle bundle = new Bundle();
						bundle.putParcelable("goods", goods);
						intent.putExtra("goods_detail", bundle);
						startActivity(intent);
					}
				});
		goCartLayout = (LinearLayout) findViewById(R.id.favorite_go_cart);
		goCartLayout.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(FavoritesActivity.this,
						CartActivity.class);
				startActivity(intent);
			}
		});
	}

	// 从数据库中查询数据并添加到adapter中
	private void addData() {
		if (!pref.isLogin()) {
			return;
		}
		adapter.clean();
		userName = pref.getUserName();
		favoriteList = dbHelper.favoriteList(userName);
		adapter.addGoods(favoriteList);
		handler.sendEmptyMessage(1);
	}

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			adapter.notifyDataSetChanged();
		}
	};

	@Override
	protected void onResume() {
		super.onResume();
		addData();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			MyApp.getInstance().exit(this);
		}
		return super.onKeyDown(keyCode, event);
	}
}
